class UF:
    def __init__ (self,count):
        self.clustercount = count
        self.total = count
        self.pid = range(count)
        self.pwt = [1]*count
    def root(self,p):
        while(p != self.pid[p]):
            self.pid[p] = self.pid[self.pid[p]]
            p = self.pid[self.pid[p]]
        return p
    def quick_find(self,p,q):
        return self.root(p) == self.root(q)
    def weighted_union(self,p,q):
        i = self.root(p)
        j = self.root(q)
        if( i != j ):
            if( self.pwt[i] < self.pwt[j]):
                self.pid[i] = j
                self.pwt[j] += self.pwt[i]
            else:
                self.pid[j] = i
                self.pwt[i] += self.pwt[j]
            self.clustercount -= 1
    def curCluster(self):
        return self.clustercount


